﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BBS.Data.Forum;
using BBS.Bll.Cache;
using System.Data;
using BBS.Entity;
using BBS.Bll.Extend;

namespace BBS.Bll.Forum
{
    public static class BoardInfo
    {
        private static readonly BoardTable _boardTable = new BoardTable();
        private static readonly ThreadTypeTable _threadTypeTable = new ThreadTypeTable();

        public static List<Board> GetAllBoard()
        {
            object obj = CacheProvider.GetObject(CacheKey.ForumBoard);
            if (null == obj)
            {
                var dt = _boardTable.GetAll();
                var list = new List<Board>();
                foreach (DataRow row in dt.Rows)
                {
                    var item = new Board();
                    item.Id = row["id"].ToInt();
                    item.Information = row["information"].ToString();
                    item.Order = row["order_number"].ToInt();
                    item.ParentId = row["parent_id"].ToInt();
                    item.Status = (StatusType)Enum.ToObject(typeof(StatusType), row["status"].ToInt());
                    item.Title = row["title"].ToString();
                    list.Add(item);
                }
                CacheProvider.AddObject(CacheKey.ForumBoard, list);
                return list;
            }
            return (List<Board>)obj;
        }
        public static void InsertBoard(Board board)
        {
            _boardTable.Insert(board);
            CacheProvider.Remove(CacheKey.ForumBoard);
        }
        public static void UpdateBoard(Board board)
        {
            _boardTable.Update(board);
            CacheProvider.Remove(CacheKey.ForumBoard);
        }
        /// <summary>
        /// 获取板块下帖子类型
        /// </summary>
        /// <param name="boardId">板块ID</param>
        /// <returns></returns>
        public static List<ThreadType> GetThreadTypeByBoardId(int boardId)
        {
            var types = GetAllThreadType();
            var foo = from bar in types where bar.BoardId == boardId select bar;
            return foo.ToList();
        }
        //获取所有类型
        private static List<ThreadType> GetAllThreadType()
        {
            object obj = CacheProvider.GetObject(CacheKey.ThreadType);
            if (null == obj)
            {
                var list = new List<ThreadType>();
                foreach (DataRow row in _threadTypeTable.GetAll().Rows)
                {
                    var item = new ThreadType();
                    item.Id = row["id"].ToInt();
                    item.Title = row["title"].ToString();
                    item.BoardId = row["board_id"].ToInt();
                    item.LastUpdateBy = row["last_update_by"].ToInt();
                    item.LastUpdateTime = row["last_update_Time"].ToString();
                    item.Status = (StatusType)Enum.ToObject(typeof(StatusType), row["status"].ToInt());
                    list.Add(item);
                }
                CacheProvider.AddObject(CacheKey.ThreadType, list);
                return list;
            }
            return (List<ThreadType>)obj;
        }

        public static void InsertThreadType(ThreadType type)
        {
            _threadTypeTable.Insert(type);
            CacheProvider.Remove(CacheKey.ThreadType);
        }
        public static void UpdateThreadType(ThreadType type)
        {
            _threadTypeTable.Update(type);
            CacheProvider.Remove(CacheKey.ThreadType);
        }

      
    }
}
